﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateDesktopDocHtmlContent
{
    internal class MoveImgsForAI
    {
        public void MoveImages(string appName,string targetNigixDir)
        {
            var currentDir = Path.GetDirectoryName(Application.ExecutablePath);
            var desktopDir = Path.Combine(currentDir, appName== "desktop" ? "agcim-desktop.doc": "agcim.ai.doc");
            desktopDir = Path.Combine(desktopDir, "docs");

             var targetDir = Path.Combine(targetNigixDir, "jike");

            var dirs=new DirectoryInfo(desktopDir).GetDirectories();
            string lastTtargetFileDirectory = null;

            for (var i=0; i< dirs.Length; i++)
            {
                var imgDir = Path.Combine(dirs[i].FullName, "img");
                if (Directory.Exists(imgDir))
                {
                    var imgs = new DirectoryInfo(imgDir).GetFiles("*.*", SearchOption.AllDirectories);
                    for (var j = 0; j < imgs.Length; j++)
                    {
                        var targetFileName = Path.Combine(targetDir, imgs[j].FullName.Substring(dirs[i].FullName.Length + 1));
                        var targetFileDirectory = Path.GetDirectoryName(targetFileName);
                        if(lastTtargetFileDirectory!= targetFileDirectory)
                        {
                            if (Directory.Exists(targetFileDirectory) == false)
                            {
                                Directory.CreateDirectory(targetFileDirectory);
                            }
                            lastTtargetFileDirectory= targetFileDirectory;
                        }
                        
                        File.Copy(imgs[j].FullName, targetFileName, true);
                    }
                }
            }
        }
    }
}
